package freedom.solution.app.prueba;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import freedom.solution.app.restaurante.datatypes.DBProducto;
import freedom.solution.app.restaurante.datatypes.DBclass;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import freedom.soluttion.app.restaurante.R;

public class CustomizedListView extends Activity {
	// All static variables
//	static final String URL = "http://api.androidhive.info/music/music.xml";
	// XML node keys
	//static final String KEY_SONG = "song"; // parent node
	static final String KEY_ID = "id";
	//static final String KEY_TITLE = "title";
	//static final String KEY_ARTIST = "artist";
	//static final String KEY_DURATION = "duration";
	static final String KEY_THUMB_URL = "thumb_url";
	
	static final String KEY_NOMBRE="nombre";
	static final String KEY_INGREDIENTES="estado";
	static final String KEY_PRECIO="codigo";
	static final String KEY_DESCRIPCION="ciclo";
	
	ArrayList<HashMap<String, String>> productosList=new ArrayList<HashMap<String,String>>();
	ListView list;
    LazyAdapter adapter;
    DBclass dbclass;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_productos);
		
		dbclass = new DBclass(getApplicationContext());
	
		
	        ArrayList<DBProducto>  productos= new ArrayList<DBProducto>();
			productos= dbclass.obtenerProductos();
					
			Iterator<DBProducto> it=productos.iterator();
		
			  
			  while ( it.hasNext() ) { 
				  Object objeto = it.next(); 
				  DBProducto dbproducto = (DBProducto)objeto; 
				   
		
				HashMap<String, String> map = new HashMap<String, String>();
				//map.put(KEY_CODCLI,obtenerPersona[i].getCodcli());
				map.put(KEY_DESCRIPCION, dbproducto.getDescripcion());
				map.put(KEY_NOMBRE, dbproducto.getNombre()+"");
				map.put(KEY_PRECIO,"S/."+dbproducto.getPrecio()+"" );
				
				map.put(KEY_THUMB_URL, dbproducto.getFoto()+"");
				
							
				productosList.add(map);
				
				
			}		
		
		

		list=(ListView)findViewById(R.id.lv_productos_main);
		
		// Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, productosList, productos);        
        list.setAdapter(adapter);
     
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

        	
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				 // On selecting single track get song information
              Intent i = new Intent(getApplicationContext(), DetalleProductoActivity.class);
 
                // to get song information
              String id_producto= ((TextView) view.findViewById(R.id.duration)).getText().toString();
                //String song_id = ((TextView) view.findViewById(R.id.song_id)).getText().toString();
 
            //    Toast.makeText(getApplicationContext(), "Album Id: " + album_id  + ", Song Id: " + song_id, Toast.LENGTH_SHORT).show();
 
             // i.putExtra("ID_PRODUCTO", id_producto);
               // i.putExtra("song_id", song_id);
 
                startActivity(i);

			}
		});		
	}	
}